#include "picketdrawable.h"

PicketDrawable::PicketDrawable()
{
    _defaultColor = QColor(0, 0, 255);
    resetColor();
}

PicketDrawable::PicketDrawable(QPointF center)
{
    _defaultColor = QColor(0, 0, 255);
    _center = center;
    resetColor();
}

void PicketDrawable::draw(CoodrsTransformer *coordsTransformer)
{
    QPointF coords = _center;
    if(coordsTransformer)
    {
        coords = coordsTransformer->transform(coords);
    }

    glPointSize(PICKET_SIZE);
    glBegin(GL_POINTS);
    glColor3f(_color.redF(), _color.greenF(), _color.blueF());
    glVertex2d(coords.x(), coords.y());
    glEnd();
}

QRectF PicketDrawable::getBoundingBox()
{
    static float modelview[16];
    glGetFloatv(GL_MODELVIEW_MATRIX, modelview);    // Fills modelview[]

    float scaleFact = modelview[0];
    float picketSize = PICKET_SIZE / scaleFact / 2;

    QPointF topLeft = QPointF((_center.x() - picketSize), (_center.y() - picketSize));
    QPointF rightBottom = QPointF((_center.x() + picketSize), (_center.y() + picketSize));
    return QRectF(topLeft, rightBottom);
}

bool PicketDrawable::isSelected(QPointF selectedPoint)
{
    QRectF boundary = getBoundingBox();
    if(boundary.contains(selectedPoint))
    {
        return true;
    }
    return false;
}
